<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
    //用户控制器 主要用于 注册 登录 退出等
    /*
    public function index(){
        $data['title']='会员中心 找金银网';
        $this->assign("data", $data);
        $this->display();
    }*/
    //报价
    public function reg(){
        //如果已登录，则跳转到后台首页
        if(session('member_id')||cookie('member_id')){
            echo "<script>alert('您已经登录，请您退出当前账号再注册');window.location.href='/home/index/'</script>";exit;
        }
        $data['title']='注册 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //添加用户 提交
    public function reg_post(){
        if($_POST['member_name']==''||$_POST['member_passwd']==''||$_POST['re_member_passwd']==''){
            echo "<script>alert('请您填写完整后尝试重新提交');history.back(-1);</script>";exit;
        }
        if($_POST['re_member_passwd']!=$_POST['member_passwd']){
            echo "<script>alert('两次输入的密码不一致，请您尝试重新输入或者联系客服人员');history.back(-1);</script>";exit;
        }
        $post=I('post.');
        //$post['email']=$post['member_name'].'@qq.com';
        //连接到UC注册
        vendor('Ucenter.UcApi');//载入UcApi扩展
        //$reg=UcApi::reg($post['member_name'], $post['member_passwd'],$post['email']);
        $reg=UcApi::reg($post['member_name'], $post['member_passwd']);
        if($reg<=0){
            echo "<script>alert('注册失败');history.back(-1);</script>";
        }
        echo "<script>alert('注册成功，欢迎您登录');window.location.href='/home/user/login';</script>";
    }
    //登录
    public function login(){
        //如果已登录，则跳转到后台首页
        if(session('member_id')||cookie('member_id')){
            echo "<script>alert('您已经登录，点击确认回到首页');window.location.href='/home/index/'</script>";exit;
        }
        $data['title']='登录 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //登录提交
    public function login_post(){
        if($_POST['member_name']==''||$_POST['member_passwd']==''){
            echo "<script>alert('请您填写完整后尝试重新提交');history.back(-1);</script>";exit;
        }
        $post=I('post.');
        vendor('Ucenter.UcApi');//载入UcApi扩展
        $login = UcApi::login($post['member_name'], $post['member_passwd']);
        if($login===FALSE){
            //$this->error(UcApi::getError());
            echo "<script>alert('登录失败，用户名或密码错误');history.back(-1);</script>";
        }
        //设置session
        $member_id=$login['uid'];
        $member_name=$login['username'];
        //$email=$login['email'];
        session('member_id',$member_id);
        session('member_name',$member_name);
        //设置cookie
        if($_POST['remember']==1){
            cookie('member_id',$member_id,3600);
        }
        echo "<script>window.location.href='/home/index'</script>";exit;
    }
    //退出登录
    public function logout(){
        session('member_id',null);
        session('member_name',null);
        cookie('member_id',null);
        echo "<script>window.location.href='/home/user/login'</script>";exit;
    }

}